<template>
  <yd-layout>
    <yd-navbar title="登录">
      <a href="javascript:;" @click="$router.go(-1)" slot="left">
        <yd-navbar-back-icon></yd-navbar-back-icon>
      </a>
    </yd-navbar>
    <yd-cell-group class="demo-small-pitch">
        <yd-cell-item>
            <span slot="left">用户名：</span>
            <yd-input slot="right" required v-model="name" max="20" placeholder="请输入用户名"></yd-input>
        </yd-cell-item>
        <yd-cell-item>
            <span slot="left">密码：</span>
            <yd-input slot="right" type="password" v-model="password" placeholder="请输入密码"></yd-input>
        </yd-cell-item>
    </yd-cell-group>
    <yd-button-group>
      <yd-button size="large" type="primary" @click.native="clickLogin">登录</yd-button>
    </yd-button-group>
    <canvas id="qrcode">

    </canvas>
  </yd-layout>
</template>

<script>
import sha1 from 'sha1'

export default {
  name: 'Login',
  data () {
    return {
      name: 'ml',
      password: '000000'
    }
  },
  methods: {
    clickLogin () {
      if (!this.password) {
        this.$message.warning('请输入密码');
        return false
      }
      if (!this.name) {
        this.$message.warning('请输入用户名');
        return false
      }
      // let pwd = sha1(this.password)
      this.ajax({
        url: this.API.common.login,
        // method: 'post',
        data: {
          password: this.password,
          name: this.name
        },
        success: (resultData) => {
          if (resultData.data && resultData.status === 0) {
            // 提示信息
            this.$message.success('登录成功');
            // 存储用户信息
            let UserInfo = {}
            UserInfo.name = this.name
            UserInfo.password = this.password
            // 有效期(秒)
            UserInfo.period = resultData.data.period
            let exper = ''
            if (UserInfo.period) {
              exper = new Date().getTime() + (UserInfo.period * 1000)
            }
            if (exper) {
              this.store.set(this.STORECONST.USERINFO, UserInfo, exper)
            } else {
              // 没有有效期 默认1小时
              this.store.set(this.STORECONST.USERINFO, UserInfo, new Date().getTime() + 1000 * 60 * 60)
            }

            let menuList = resultData.data.menuAdminList;
            this.store.set(this.STORECONST.USERMENU, menuList, new Date().getTime() + 1000 * 60 * 60)

            // 登录成功后跳转
            this.$router.push(this.$route.query.redirect || '/')
          } else {
            this.$message.error(resultData.msg);
          }
        }
      })
    }
  }
}
</script>


<style>

</style>
